package com.itboye.lottery.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

import com.google.gson.Gson;
import com.itboye.lottery.MainActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.user.EditPhoneActivity;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.app.MyApplication;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.bean.UserDataBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.Const;
import com.itboye.lottery.utils.SPUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.jetbrains.annotations.NotNull;


public class WXEntryActivity extends BaseActivity implements IWXAPIEventHandler, ResponseCallback {

    private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;

    private Button gotoBtn, regBtn, launchBtn, checkBtn, scanBtn;

    private IWXAPI api;
    MyApplication app;
    UserPresenter userPresenter = new UserPresenter(this);
    private String wxcode = "";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        app = (MyApplication) getApplication();
        api = MyApplication.Companion.getInstance().getIwxapi();
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        int result = 0;
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                if (resp.transaction.equals(Const.Companion.getWX_LOGIN())) {
                    wxcode = ((SendAuth.Resp) resp).code;
                    System.out.println("微信登录code:" + wxcode);
                    //是否注册过
                    userPresenter.isRegister(wxcode);
//                    userPresenter.wxLogin(wxcode, "", "", "");
                } else if (resp.transaction.equals("webpage")) {

                }
                result = R.string.login_success;
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = R.string.errcode_cancel;
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = R.string.errcode_deny;
                finish();
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                result = R.string.errcode_unsupported;
                finish();
                break;
            default:
                result = R.string.errcode_unknown;
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initData() {

    }


    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.WX_LOGIN_SUCCESS) {
            ByAlert.alert(getString(R.string.login_success));
            UserDataBean userDataBean = (UserDataBean) o;
            SPUtils.put(this,userDataBean);
            Intent intent = new Intent(userPresenter.WX_LOGIN_SUCCESS);
            sendBroadcast(intent);
            startActivity(new Intent(this, MainActivity.class));
        } else if (requestSuccessType == userPresenter.isRegister_SUCCESS) {
            UserDataBean userDataBean = (UserDataBean) o;
            if (userDataBean.getIs_register() == 0) {
                //没有注册
                String json=new Gson().toJson(userDataBean);
                startActivity(new Intent(this, EditPhoneActivity.class).putExtra("type", 5).putExtra("code", json));
            } else {
                //已经注册过
                SPUtils.put(this,userDataBean);
                Intent intent = new Intent(userPresenter.WX_LOGIN_SUCCESS);
                sendBroadcast(intent);
                startActivity(new Intent(this, MainActivity.class));
            }
        }
        finish();
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        if (requestErrorType == userPresenter.isRegister_FAIL) {
            ByAlert.alert(msg);
        } else if (requestErrorType == userPresenter.WX_LOGIN_FAIL) {
            ByAlert.alert(msg);//微信登录失败
        }
        finish();
    }
}